#include "contract.h"
#include "gd32f30x.h"

void Timer_Tick(void);


/**
  * @brief  This function handles TIMER1 interrupt request.
  * @param  None
  * @retval None
  */
void TIMER1_IRQHandler(void)
{
	timer_interrupt_flag_clear(TIMER1, TIMER_INT_FLAG_UP);		
	Timer_Tick();
}


/*
初始化定时器，默认周期10ms
*/
void BSP_Timer_Init(void){
	timer_parameter_struct timer_initpara;
	rcu_periph_clock_enable(RCU_TIMER1);
	timer_deinit(TIMER1);
	uint32_t clk = rcu_clock_freq_get(CK_APB1)*2;
	
	/* TIMER1 configuration */
	timer_initpara.prescaler         = clk / 10000U - 1;
	timer_initpara.alignedmode       = TIMER_COUNTER_CENTER_UP;
	timer_initpara.counterdirection  = TIMER_COUNTER_UP;
	timer_initpara.period            = 10;
	timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
	timer_init(TIMER1, &timer_initpara);
	
	timer_interrupt_enable(TIMER1, TIMER_INT_UP);
		
	nvic_irq_enable(TIMER1_IRQn, 1, 0);

	timer_enable(TIMER1);
}

void BSP_Timer_DeInit(void)
{
	timer_disable(TIMER1);
}

uint32_t BSP_Timer_Count(void){
	return timer_counter_read(TIMER1);
}


